﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using HRM.DAL.DAO;

namespace HRM.DAL.Gateway
{
    public class LeaveGateway:DBGateway
    {
        public bool AlreadySetup(Leave leave)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblLeave";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Error in Server ! Try Later ");
            }
            finally
            {
                ConnectionObj.Close();
            }
            return false;
        }

        public bool ConfigureLeave(Leave leave)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblLeave values(@earn,@seek,@casual,@others)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@earn", SqlDbType.Int).Value = leave.EarnLeave;
                CommandObj.Parameters.Add("@seek", SqlDbType.Int).Value = leave.SickLeave;
                CommandObj.Parameters.Add("@casual", SqlDbType.Int).Value = leave.CasualLeave;
                CommandObj.Parameters.Add("@others", SqlDbType.Int).Value = leave.Others;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error occured during Configuring Leave !");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public bool UpdateLeaveSetup(Leave leave)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("Update tblLeave set Earn=@earn,Seek=@seek,Casual=@casual,Others=@others");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@earn", SqlDbType.Int).Value = leave.EarnLeave;
                CommandObj.Parameters.Add("@seek", SqlDbType.Int).Value = leave.SickLeave;
                CommandObj.Parameters.Add("@casual", SqlDbType.Int).Value = leave.CasualLeave;
                CommandObj.Parameters.Add("@others", SqlDbType.Int).Value = leave.Others;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error occured during Configuring Leave !");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public Leave GetLeaveSettings()
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblLeave";
                SqlDataReader reader = CommandObj.ExecuteReader();
                Leave leave=new Leave();
                while (reader.Read())
                {
                    leave.EarnLeave = Convert.ToInt32(reader[1]);
                    leave.SickLeave = Convert.ToInt32(reader[2]);
                    leave.CasualLeave = Convert.ToInt32(reader[3]);
                    leave.Others = Convert.ToInt32(reader[4]);
                }
                return leave;
            }
            catch (Exception)
            {
                throw new Exception("Error in Server ! Try Later ");
            }
            finally
            {
                ConnectionObj.Close();
            }
          
        }

        public bool ResetLeave(Leave leave)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("Update tblLeaveStatus set Earn=@earn,Seek=@sick,Casual=@casual,Others=@others");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@earn", SqlDbType.Int).Value = leave.EarnLeave;
                CommandObj.Parameters.Add("@sick", SqlDbType.Int).Value = leave.SickLeave;
                CommandObj.Parameters.Add("@casual", SqlDbType.Int).Value = leave.CasualLeave;
                CommandObj.Parameters.Add("@others", SqlDbType.Int).Value = leave.Others;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error Occured Ouring Reseting Leave Status !");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}